package renxian;

import renxian.AboutDialog;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;

public class SpiderMenuBar extends JMenuBar {
    //生成spider框架对象
    Spider main = null;

    //生成菜单组
    JMenu jNewGame = new JMenu("游戏");
    JMenu jHelp = new JMenu("帮助");
    JMenu jTheme = new JMenu("换主题");

    //生成菜单项
    JMenuItem jItemAbout = new JMenuItem("关于");
    JMenuItem jItemOpen = new JMenuItem("新游戏");
    JMenuItem jItemPlayAgain = new JMenuItem("发牌");

    //生成单选框
    JRadioButtonMenuItem jRMItemEasy = new JRadioButtonMenuItem("简单：单一花色");
    JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuItem("中级：双花色");
    JRadioButtonMenuItem jRMItemHard = new JRadioButtonMenuItem("高级：四花色");

    JMenuItem jItemExit = new JMenuItem("退出");
    JMenuItem jItemHint = new JMenuItem("提示");

    public SpiderMenuBar(Spider spider) {
        this.main = spider;

        // 初始化“游戏”菜单栏
        jNewGame.add(jItemOpen);
        jNewGame.add(jItemPlayAgain);
        jNewGame.addSeparator();
        jNewGame.add(jRMItemEasy);
        jNewGame.add(jRMItemNormal);
        jNewGame.add(jRMItemHard);
        jNewGame.addSeparator();
        jNewGame.add(jItemHint);
        jNewGame.add(jItemExit);

        // 新增主题菜单项
        jTheme.add(createThemeMenuItem("粉色", Color.PINK));
        jTheme.add(createThemeMenuItem("绿色", new Color(204,255,204)));

        jTheme.add(createThemeMenuItem("蓝色", new Color(204,255,255) ));
        jNewGame.add(jTheme);

        ButtonGroup group = new ButtonGroup();
        group.add(jRMItemEasy);
        group.add(jRMItemNormal);
        group.add(jRMItemHard);

        jHelp.add(jItemAbout);

        this.add(jNewGame);
        this.add(jHelp);

        // 为组件添加事件监听并实现
        jItemOpen.addActionListener(e -> main.newGame());
        jItemPlayAgain.addActionListener(e -> {
            if (main.getC() < 60) {
                main.deal();
            }
        });
        jItemExit.addActionListener(e -> {
            main.dispose();
            System.exit(0);
        });

        jRMItemEasy.setSelected(true);
        jRMItemEasy.addActionListener(e -> {
            main.setGrade(Spider.EASY);
            main.initCards();
            main.newGame();
        });
        jRMItemNormal.addActionListener(e -> {
            main.setGrade(Spider.NATURAL);
            main.initCards();
            main.newGame();
        });
        jRMItemHard.addActionListener(e -> {
            main.setGrade(Spider.HARD);
            main.initCards();
            main.newGame();
        });

        jItemHint.addActionListener(e -> main.showHint());

        jNewGame.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuSelected(javax.swing.event.MenuEvent e) {
                jItemPlayAgain.setEnabled(main.getC() < 60);
            }
            public void menuDeselected(javax.swing.event.MenuEvent e) {}
            public void menuCanceled(javax.swing.event.MenuEvent e) {}
        });

        jItemAbout.addActionListener(e -> new AboutDialog());
    }

    // 创建主题菜单项
    private JMenuItem createThemeMenuItem(String name, Color color) {
        JMenuItem menuItem = new JMenuItem(name);
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                main.getContentPane().setBackground(color);
            }
        });
        return menuItem;
    }

    class Show extends Thread {
        public void run() {
            main.showEnableOperator();
        }
    }
}
